package com.youzheng.controller;

import com.youzheng.entity.User;
import com.youzheng.service.IUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class LoginController {
    @Resource
    private IUserService userService;
    @RequestMapping("/toLogin.do")
    public String toLogin(){
        return "login";
    }
    @RequestMapping("/login.do")
    public String login(String username, String password, ModelMap map, HttpSession session){
        User user=userService.login(username,password);
        if(user!=null){
            session.setAttribute("user",user);
            return "redirect:/product/list.do";
        }else{
            map.addAttribute("login_msg","用户名或密码错误");
            return "login";
        }
    }
    @RequestMapping("/logout.do")
    public String logout(HttpSession session){
        session.removeAttribute("user");
        session.invalidate();
        return "login";
    }
    @RequestMapping("/tonewuser.do")
    public String tonewuser(){
        System.out.println("123");
        return "newuser";
    }
    @RequestMapping("/newuser.do")
    public String newuser(String username, String password,int age,String sex,String real_name){
        userService.addnewUser(username,password,age,sex,real_name);
        return "success";
    }
}
